﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using PessMvc.Helpers;
using System.Web.Security;

namespace PessMvc.Models
{
    public class ExpertJudgeInfoComplete : AuthorizeAttribute 
    {
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            //        return RedirectToAction("Edit", "ExpertJudge", new { serialNumber = model.UserName, returnUrl = returnUrl });
            FormsAuthentication.SignOut();
            HttpContext.Current.Session["ActivityID"] = -1;
            //return RedirectToAction("Index", "Activity");
            filterContext.Result = new RedirectToRouteResult(
                new RouteValueDictionary
                {
                    {"action","Logon"},
                    {"controller","Account"}                    
                });
        }
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (System.Web.Security.Roles.IsUserInRole("ExpertJudge"))
            {
                return Utility.IsExpertJudgeInfoComplete(Membership.GetUser().UserName);
            }
            if (System.Web.Security.Roles.IsUserInRole("Admin"))
            {
                return true;
            }
                
            return base.AuthorizeCore(httpContext);
        }
    }

    public class ExpertContractSigned : AuthorizeAttribute
    {
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            //        return RedirectToAction("Edit", "ExpertJudge", new { serialNumber = model.UserName, returnUrl = returnUrl });
            //FormsAuthentication.SignOut();
            HttpContext.Current.Session["ActivityID"] = -1;
            //return RedirectToAction("Index", "Activity");
            filterContext.Result = new RedirectToRouteResult(
                new RouteValueDictionary
                {
                    {"action","Sign"},
                    {"controller","Contract"}                    
                });
        }
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (System.Web.Security.Roles.IsUserInRole("ExpertJudge"))
            {
                return Utility.IsContractSigned(Membership.GetUser().UserName);
            }
            else if (System.Web.Security.Roles.IsUserInRole("Admin"))
            {
                return true;
            }

            return base.AuthorizeCore(httpContext);
        }
    }
}